//
//  ShufView.m
//  轮播图
//
//  Created by dllo on 16/8/4.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ShufView.h"
#import "UIImageView+WebCache.h"


#define kShufCollectionCellReuse @"shufCollectionViewCell"
#define kCollectionWidth _collectionView.frame.size.width
#define kCollectionHeight _collectionView.frame.size.height

#pragma mark <!------------- ShufView -------------->
@interface ShufView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;

@property (nonatomic, strong) NSTimer *timer;

@end

@implementation ShufView

- (instancetype)initWithFrame:(CGRect)frame
{
    return [self initWithFrame:frame andImages:nil];
}

- (instancetype)initWithFrame:(CGRect)frame andImages:(NSArray<NSString *> *)images
{
    self = [super initWithFrame:frame];
    if (self) {
        _images = images;
        [self createCollectionView];
        [self createPageControl];
        [self settingOthers];
        [self createTimer];
    }
    return self;
}


- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        
        [self createCollectionView];
        [self createPageControl];
        [self settingOthers];
        [self createTimer];
    }
    return self;
}

- (void)settingOthers
{
    _imageLocation.left = 10;
    _imageLocation.bottom = 10;
    _imageLocation.maxLength = 180;
    _imageLocation.height = 20;
}

- (void)setImages:(NSArray *)images
{
    _images = images;
    _pageControl.numberOfPages = _images.count;
    [_collectionView reloadData];
}

#pragma mark - Timer
- (void)createTimer
{
//    _timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerExecute:) userInfo:nil repeats:YES];
    // 仅仅是创建，并没有加在runloop中
    _timer = [NSTimer timerWithTimeInterval:3 target:self selector:@selector(timerExecute:) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
    //[_timer fire];
}

- (void)timerExecute:(NSTimer *)timer
{
    [_collectionView setContentOffset:CGPointMake(_collectionView.contentOffset.x + kCollectionWidth, 0) animated:YES];
    [self currentPageControl];
    
    if ((int)(_collectionView.contentOffset.x / kCollectionWidth) % _images.count == _images.count - 1) {
        _pageControl.currentPage = 0;
        if (_collectionView.contentOffset.x > 48 * kCollectionWidth * _images.count - kCollectionWidth) {
            [_collectionView setContentOffset:CGPointMake(kCollectionWidth * _images.count * 2 , 0) animated:NO];
        }
        return;
    }

    _pageControl.currentPage += 1;
    _pageControl.currentPage = _pageControl.currentPage % _images.count;
}

#pragma mark - create subview
- (void)createCollectionView
{
    _layout = [[UICollectionViewFlowLayout alloc]init];
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:_layout];
    [self addSubview:_collectionView];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.pagingEnabled = YES;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    
    _layout.minimumLineSpacing = 0;
    _layout.minimumInteritemSpacing = 0;
    _layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    [_collectionView registerClass:[ShufCollectionViewCell class] forCellWithReuseIdentifier:kShufCollectionCellReuse];
}

- (void)createPageControl
{
    _pageControl = [[UIPageControl alloc]init];
    [self addSubview:_pageControl];
    _pageControl.currentPage = 0;
    
    
}



#pragma mark - collection view data source
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _images.count * 50;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ShufCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kShufCollectionCellReuse forIndexPath:indexPath];
    
    cell.image = [_images objectAtIndex:indexPath.item % _images.count];
    UIShufImageLocation imageLocation = self.imageLocation;
    cell.title.imageLocation = imageLocation;
    [self.delegate cellSubviewStyle:cell];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (collectionView.contentOffset.x == 0) {
        _collectionView.contentOffset = CGPointMake(kCollectionWidth * _images.count * 2, 0);
    }
}

#pragma mark - scroll view delegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [_timer setFireDate:[NSDate distantFuture]];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (_collectionView.contentOffset.x > 48 * kCollectionWidth * _images.count - kCollectionWidth) {
        [_collectionView setContentOffset:CGPointMake(kCollectionWidth * _images.count * 2 , 0) animated:NO];
    } else if (_collectionView.contentOffset.x < 1 * kCollectionWidth * _images.count) {
        [_collectionView setContentOffset:CGPointMake(kCollectionWidth * _images.count * 2 - kCollectionWidth , 0) animated:NO];
    }
    [self currentPageControl];
    [_timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:3]];
}

- (void)currentPageControl
{
    _pageControl.currentPage = (int)(_collectionView.contentOffset.x / kCollectionWidth) % _images.count;
}

#pragma mark - layout subviews
- (void)layoutSubviews
{
    [super layoutSubviews];
    _collectionView.frame = self.bounds;
    _layout.itemSize = _collectionView.frame.size;
    _pageControl.frame = CGRectMake(0, 0, 0, 20);
    CGPoint center = CGPointMake(_collectionView.center.x, CGRectGetHeight(self.frame) - 10);
    _pageControl.center = center;
}


@end



#pragma mark <!------------- ShufCollectionViewCell -------------->
@interface ShufCollectionViewCell ()



@end

@implementation ShufCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews];
    }
    return self;
}

- (void)createSubviews
{
    _imageView = [[UIImageView alloc]init];
    [self.contentView addSubview:_imageView];
    _imageView.userInteractionEnabled = YES;
    
    _title = [[ShufLabel alloc]init];
    [self.contentView addSubview:_title];
    _title.userInteractionEnabled = YES;
    _title.textColor = [UIColor blackColor];
    _title.font = [UIFont systemFontOfSize:15];
    
}

- (void)setImage:(NSString *)image
{
    
    
    _image = image;
    [_imageView sd_setImageWithURL:[NSURL URLWithString:image] placeholderImage:[UIImage imageNamed:@"placeholder"]];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.imageView.frame = self.contentView.bounds;
    self.title.frame = CGRectMake(_title.imageLocation.left, CGRectGetHeight(self.contentView.frame) - _title.imageLocation.height - _title.imageLocation.bottom, _title.imageLocation.maxLength, _title.imageLocation.height);
}

@end

#pragma mark <!------------- ShufLabel -------------->
@implementation ShufLabel


@end
